package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairWorkdetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbLegacyIssuesBo;
import com.ruoyi.tb.lmt.domain.vo.TbLegacyIssuesVo;
import com.ruoyi.tb.lmt.domain.TbLegacyIssues;
import com.ruoyi.tb.lmt.mapper.TbLegacyIssuesMapper;
import com.ruoyi.tb.lmt.service.ITbLegacyIssuesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 遗留问题Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-15
 */
@RequiredArgsConstructor
@Service
public class TbLegacyIssuesServiceImpl implements ITbLegacyIssuesService {

    private final TbLegacyIssuesMapper baseMapper;

    @Autowired
    private  TbLegacyIssuesMapper tbLegacyIssuesMapper;

    /**
     * 查询遗留问题
     */
    @Override
    public TbLegacyIssuesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询遗留问题列表
     */
    @Override
    public TableDataInfo<TbLegacyIssuesVo> queryPageList(TbLegacyIssuesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbLegacyIssues> lqw = buildQueryWrapper(bo);
        Page<TbLegacyIssuesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询遗留问题列表
     */
    @Override
    public List<TbLegacyIssuesVo> queryList(TbLegacyIssuesBo bo) {
        LambdaQueryWrapper<TbLegacyIssues> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbLegacyIssues> buildQueryWrapper(TbLegacyIssuesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbLegacyIssues> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWorkdetailId() != null, TbLegacyIssues::getWorkdetailId, bo.getWorkdetailId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbLegacyIssues::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssues()), TbLegacyIssues::getLegacyIssues, bo.getLegacyIssues());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesData()), TbLegacyIssues::getLegacyIssuesData, bo.getLegacyIssuesData());
        lqw.eq(bo.getLegacyIssuesStatus() != null, TbLegacyIssues::getLegacyIssuesStatus, bo.getLegacyIssuesStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesSolution()), TbLegacyIssues::getLegacyIssuesSolution, bo.getLegacyIssuesSolution());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesRemark()), TbLegacyIssues::getLegacyIssuesRemark, bo.getLegacyIssuesRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesMaterial()), TbLegacyIssues::getLegacyIssuesMaterial, bo.getLegacyIssuesMaterial());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesNumber()), TbLegacyIssues::getLegacyIssuesNumber, bo.getLegacyIssuesNumber());
        lqw.eq(bo.getCreateTime() != null, TbLegacyIssues::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbLegacyIssues::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbLegacyIssues::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbLegacyIssues::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增遗留问题
     */
    @Override
    public Boolean insertByBo(TbLegacyIssuesBo bo) {
        TbLegacyIssues add = BeanUtil.toBean(bo, TbLegacyIssues.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改遗留问题
     */
    @Override
    public Boolean updateByBo(TbLegacyIssuesBo bo) {
        TbLegacyIssues update = BeanUtil.toBean(bo, TbLegacyIssues.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbLegacyIssues entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除遗留问题
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TbLegacyIssuesVo> selectList(String status, PageQuery pageQuery) {
        List<TbLegacyIssuesVo> result = tbLegacyIssuesMapper.selectList(status);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLegacyIssuesVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }

    }

    @Override
    public TbLegacyIssuesVo selectById(Long id) {
        return tbLegacyIssuesMapper.selectListById(id);
    }

    @Override
    public TableDataInfo<TbLegacyIssuesVo> selectExtractionList(String status, PageQuery pageQuery) {
        List<TbLegacyIssuesVo> result = tbLegacyIssuesMapper.selectExtractionList(status);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbLegacyIssuesVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }
}
